package module

import (
    "github.com/gin-gonic/gin"
    "strconv"
    "yi-shou-backstage/errmsg"
    "yi-shou-backstage/model"
    "yi-shou-backstage/util"
    "yi-shou-backstage/module/param"
    "yi-shou-backstage/module/result"
)

func PostCommentControl(req *gin.Context) (int, string, interface{}) {
    user_info := util.GetUserInfo(req)
    if user_info == nil {
        util.LogEntry.Errorf("token不存在或已过期 \n")
        return errmsg.TOKEN_IS_EXPIRED, errmsg.GetErrorByCode(errmsg.TOKEN_IS_EXPIRED), nil
    }

    // 获取参数
    var params param.CreateCommentParam
    err := req.ShouldBindJSON(&params)
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 校验数据
    if ok, msg := util.Validator(&params); !ok {
        util.LogEntry.Errorf("数据校验失败, error = %s, param = %s \n", err, params)
        return errmsg.VALIDATE_ERROR, msg, nil
    }

    // 判断是添加评论，还是回复评论
    var obj model.Comment
    if params.Target != 0 {  // 回复评论
        obj = model.Comment{
            Content: params.Content,
            ArticleID: params.ArticleID,
            ParentID: params.Target,
            UserID: user_info.UserID,
        }
    }else {  // 新增评论
        obj = model.Comment{
            Content: params.Content,
            ArticleID: params.ArticleID,
            UserID: user_info.UserID,
        }
    }
    data := model.CreateComment(obj)
    if data == nil {
        util.LogEntry.Errorf("添加评论失败，error = %s \n")
        return errmsg.CREATE_COMMENT_ERROR, errmsg.GetErrorByCode(errmsg.CREATE_COMMENT_ERROR), nil
    }

    resp := result.CreateCommentResponse{ComID: data.ParentID, Target: data.ID, ArtID: data.ArticleID}
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), resp
}

func PutCommentStickyControl(req *gin.Context) (int, string, interface{}) {
    // 校验权限（此处假定只有admin用户有权限，实际的权限校验会更复杂）
    user_info := util.GetUserInfo(req)
    if user_info == nil {
        util.LogEntry.Errorf("token不存在或已过期 \n")
        return errmsg.TOKEN_IS_EXPIRED, errmsg.GetErrorByCode(errmsg.TOKEN_IS_EXPIRED), nil
    }
    user := model.GetUser(user_info.UserID)
    if user == nil {
        util.LogEntry.Errorf("该用户不存在, user_id = %s \n", user_info.UserID)
        return errmsg.USER_NOT_EXIST, errmsg.GetErrorByCode(errmsg.USER_NOT_EXIST), nil
    }
    if user.Name != "admin" {
        util.LogEntry.Errorf("该用户没有权限 \n")
        return errmsg.NO_PERMISSION, errmsg.GetErrorByCode(errmsg.NO_PERMISSION), nil
    }

    // 获取参数
    id, err := strconv.Atoi(req.Param("target"))
    if err != nil {
        util.LogEntry.Errorf("数据类型转换错误, error = %s \n", err)
        return errmsg.DATA_TYPE_CONV_ERROR, errmsg.GetErrorByCode(errmsg.DATA_TYPE_CONV_ERROR), nil
    }

    var params model.Comment
    err = req.ShouldBindJSON(&params)
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 修改评论置顶
    data := model.UpdateCommentSticky(id, params)
    if data == nil {
        util.LogEntry.Errorf("修改评论置顶失败")
        return errmsg.UPDATE_COMMENT_STICKY_ERROR, errmsg.GetErrorByCode(errmsg.UPDATE_COMMENT_STICKY_ERROR), nil
    }

    resp := result.PutCommentStickyResponse{Target: data.ID, Sticky: data.Sticky}
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), resp
}